package com.mywang.weitravel.modules.study;

import com.mywang.weitravel.base.BasePresenter;
import com.mywang.weitravel.model.StudyBean;
import com.mywang.weitravel.model.StudyDetailBean;
import com.mywang.weitravel.net.RetrofitHelper;
import com.mywang.weitravel.net.RxSchedulers;
import com.mywang.weitravel.net.VObserver;

import java.util.HashMap;
import java.util.List;

/**
 * Created by wangmingyun on 2019/12/11.
 */
public class StudyPresenter extends BasePresenter<StudyContract.View> implements StudyContract.Presenter {

    public StudyPresenter(StudyContract.View view) {
        super(view);
    }

    @Override
    public void getData() {
        mView.showLoading();
        RetrofitHelper.getInstance().getServer()
                .getStudyList()
                .compose(RxSchedulers.applySchedulers())
                .as(bindToLife())
                .subscribe(new VObserver<List<StudyBean>>() {
                    @Override
                    protected void onSuccess(List<StudyBean> list) {
                        mView.onSuccess(list);
                        mView.hideLoading();
                    }

                    @Override
                    protected void onFailure(int code, String message, boolean isShowMsg) {
                        super.onFailure(code, message, isShowMsg);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getStudyDetail(String id) {
        mView.showLoading();
        HashMap<String, String> params = new HashMap<>();
        params.put("study_id", id);
        RetrofitHelper.getInstance().getServer()
                .getStudyDetail(params)
                .compose(RxSchedulers.applySchedulers())
                .as(bindToLife())
                .subscribe(new VObserver<StudyDetailBean>() {
                    @Override
                    protected void onSuccess(StudyDetailBean bean) {
                        mView.onDetailSuccess(bean);
                        mView.hideLoading();
                    }

                    @Override
                    protected void onFailure(int code, String message, boolean isShowMsg) {
                        super.onFailure(code, message, isShowMsg);
                        mView.hideLoading();
                    }
                });
    }
}
